<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
</head>
<body>
<input name="username" value="sam1">username</input>
<input id="file" name="userfile" type="file"></input>
<button onclick="handleOnclick()">提交</button>
<script src="/node_modules/promise-polyfill/promise.min.js"></script>
<script src="../formdata.js"></script>
<script type="text/javascript">
var handleOnclick = function(){
    var formData = new FormDataPolyfill();
    formData.append("username", "sam");
    // HTML file input, chosen by user
    formData.append("userfile", document.querySelector("#file").files[0]);
    // JavaScript file-like object
    var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
    var blob = new Blob([content], { type: "text/xml"});
    formData.append("webmasterfile", blob);

    postData(formData).then(function(res){
        console.log(res)
    })
}

var postData = function(formData){
    // use fetch
    if (formData.polyfill) {
        return formData.toString().then(function(data){
            return fetch("/", {
                method: "POST",
                headers: {
                    "Content-Type": "multipart/form-data; boundary=" + formData.boundary
                },
                body: data
            })
        })
    } else {
       return fetch("/", {
            method: "POST",
            body: datda
       })
    }
}

</script>
</body>
</html>

